package com.iamteer.day23;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ChatReceiver extends Thread {

	@Override
	public void run() {
		try {
			DatagramSocket socket = new DatagramSocket(9090);
			byte[] buf = new byte[1024];
			DatagramPacket packet = new DatagramPacket(buf, buf.length);
			boolean flag = true;
			while (flag) {
				socket.receive(packet);
				String ip = packet.getAddress().getHostAddress();
				String msg = new String(buf, 0, packet.getLength());
				System.out.println("[" + ip + "] 说：" + msg);
			}
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
